package cn.net.wanji.controller;

import cn.net.wanji.annotation.DataSource;
import cn.net.wanji.domain.AjaxResult;
import cn.net.wanji.entity.TrafficFlowVehicleDto;
import cn.net.wanji.entity.VehicleRsuInfo;
import cn.net.wanji.enums.DataSourceType;
import cn.net.wanji.service.BookProducerService;
import cn.net.wanji.service.TrafficFlowStatisticsService;
import cn.net.wanji.utils.KafkaUtil;
import cn.net.wanji.utils.PageUtil;
import cn.net.wanji.vo.VehicleExceptionVo;
import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@Log4j2
@RestController
@RequestMapping(value = "/statistics")
public class TrafficFlowStatisticsController{
    @Value("${kafka.topic.my-topic}")
    String myTopic;
    BookProducerService producer;
    TrafficFlowStatisticsController(BookProducerService producer) {
        this.producer = producer;
    }
    @Autowired
    private TrafficFlowStatisticsService statisticsService;
    // 车流量统计，包括客车和货车的信息
    @GetMapping("/trafficFlowVehicleClassifiedStatistic")
    @DataSource(value = DataSourceType.SLAVE)
    public AjaxResult trafficFlowVehicleClassifiedStatistic(VehicleRsuInfo vehicleRsuInfo) {
        Map<String,List<TrafficFlowVehicleDto>> vehicleExceptionVos = statisticsService.trafficFlowVehicleClassifiedStatistic(vehicleRsuInfo);

        return  AjaxResult.success(vehicleExceptionVos);
    }
    // 异常车辆。显示车型异常和标签异常的
    @GetMapping("/abnormalmodels")
    @DataSource(value = DataSourceType.SLAVE)
    public AjaxResult abnormalModels(VehicleRsuInfo vehicleRsuInfo) {
        VehicleExceptionVo abnormalmodels = statisticsService.abnormalmodels(vehicleRsuInfo);
        VehicleExceptionVo labelAbnormality = statisticsService.labelAbnormality(vehicleRsuInfo);
        Map<String, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("abnormalmodels", abnormalmodels);
        objectObjectHashMap.put("labelAbnormality", labelAbnormality);
        return  AjaxResult.success(objectObjectHashMap);
    }
    // 五个两客一危辆显示
    @GetMapping("/hazardousChemicals")
    @DataSource(value = DataSourceType.SLAVE)
    public AjaxResult hazardousChemicals(VehicleRsuInfo vehicleRsuInfo) {
        PageUtil.startPage(1, 10);
        List<VehicleRsuInfo>  hazardousChemicals = statisticsService.hazardousChemicals(vehicleRsuInfo);
        return  AjaxResult.success(hazardousChemicals);
    }
    // 顶端显示的累计过车、近一小时过车、累计异常车辆、车型不一致、危化品车辆
    @GetMapping("/vehicleRsuInfos")
    @DataSource(value = DataSourceType.SLAVE)
    public AjaxResult vehicleRsuInfos(VehicleRsuInfo vehicleRsuInfo) {
        Map<String, Long> stringLongMap = statisticsService.vehicleRsuInfos(vehicleRsuInfo);
        return  AjaxResult.success(stringLongMap);
    }
    // 小时过车柱状图统计
    @GetMapping("/getFlowStatementList")
    @DataSource(value = DataSourceType.SLAVE)
    public AjaxResult getFlowStatementList(VehicleRsuInfo vehicleRsuInfo) {
        List<Map<String, String>> stringLongMap = null;
        try {
            stringLongMap = statisticsService.getFlowStatementList(vehicleRsuInfo);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return  AjaxResult.success(stringLongMap);
    }

    @GetMapping("/getys")
    @DataSource(value = DataSourceType.SLAVE)
    public AjaxResult getys() {
        this.producer.sendMessage(myTopic, "1111111111");
        return  AjaxResult.success();
    }
}
